package com.fyakm.daodejing.stock;

import com.fyakm.daodejing.simple.stockTest.util.StockTradingUtils;
import com.fyakm.daodejing.simple.stockTest.util.StockUtil;
import com.fyakm.daodejing.stock.domain.StockHistoryItem;
import com.fyakm.daodejing.stock.service.Backtester;
import com.fyakm.daodejing.stock.service.StockTradingStrategy;
import com.fyakm.daodejing.stock.service.impl.MovingAverageCrossStrategy;
import com.fyakm.daodejing.stock.util.CsvReader;
import com.fyakm.daodejing.stock.util.StockHistoryUtil;
import org.junit.Test;

import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class TestStock {


    @Test
    // 测试均线交叉策略
    public void TeseMovingeCrossStrategy(){

        // 股票数据目录
        String dir = "D:\\share\\A股数据";

        // 读取目录下的所有股票文件
        File directory = new File(dir);
        File[] files = directory.listFiles((dir1, name) -> name.endsWith(".csv"));


        // 随机随机跳出files中的一个file
        int index = (int) (Math.random() * files.length);
        File testFile = files[index];

        // 打印测试文件的路径
        System.out.println("测试文件路径：" + testFile.getPath());

        // 读取股票数据
        List<StockHistoryItem> historyItems = CsvReader.readStockHistoryFromCsv(testFile.getPath());


        // 定义日期字符串
        String startDateStr = "2024-01-01";
        String endDateStr = "2025-01-01";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date startDate = sdf.parse(startDateStr);
            Date endDate = sdf.parse(endDateStr);
            historyItems = StockHistoryUtil.splitStockHistoryByDate(historyItems,startDate,endDate);

            System.out.println("读取到的额长度：" + historyItems.size());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        // 创建 SimpleDateFormat 对象，指定日期格式

        // 模拟历史数据
//        List<StockHistoryItem> historyItems = new ArrayList<>();

// 定义买卖数量
        BigDecimal buyQuantity = new BigDecimal("100");
        BigDecimal sellQuantity = new BigDecimal("100");
        // 定义初始资金
        BigDecimal initialCapital = new BigDecimal("10000");

        // 创建不同的策略实例
        StockTradingStrategy maCrossStrategy = new MovingAverageCrossStrategy(5, 20, buyQuantity, sellQuantity);
//        StockTradingStrategy fixedConditionStrategy = new FixedConditionStrategy(buyQuantity, sellQuantity);

        // 对移动平均线交叉策略进行回测
        Backtester maCrossBacktester = new Backtester(maCrossStrategy, initialCapital);
        BigDecimal maCrossFinalCapital = maCrossBacktester.backtest(historyItems);

        // 对固定条件策略进行回测
//        Backtester fixedConditionBacktester = new Backtester(fixedConditionStrategy, initialCapital);
//        BigDecimal fixedConditionFinalCapital = fixedConditionBacktester.backtest(historyItems);

        // 输出回测结果
        System.out.println("移动平均线交叉策略回测结果：");
        System.out.println("初始资金: " + initialCapital);
        System.out.println("最终资金: " + maCrossFinalCapital);
        System.out.println("收益: " + maCrossFinalCapital.subtract(initialCapital));

//        System.out.println("\n固定条件策略回测结果：");
//        System.out.println("初始资金: " + initialCapital);
//        System.out.println("最终资金: " + fixedConditionFinalCapital);
//        System.out.println("收益: " + fixedConditionFinalCapital.subtract(initialCapital));
    }

}
